@extends('admin.layouts.base')

@section('title','后台管理系统')

@section('css')
    <link rel="stylesheet" href="/excel/css/handsontable.full.css">
    <link rel="stylesheet" href="/excel/css/bootstrap-excel-tab.css">
@endsection

@section('js')
    <script type="text/javascript">
        $.ajaxSetup({
            headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' }
        });
        var select_src = {row:-1,col:-1};
        var select_des = {row:-1,col:-1};
        var sheets = [];
        var empty_sheet = JSON.parse('{!! $empty_sheet !!}');
    </script>
    <!--<script language="JavaScript" src="/excel/jquery-1.10.2.js"></script>-->
    <script language="JavaScript" src="/excel/handsontable.full.js"></script>
    <script language="JavaScript" src="/excel/lodash.js"></script>
    <script language="JavaScript" src="/excel/underscore.string.js"></script>
    <script language="JavaScript" src="/excel/moment.js"></script>
    <script language="JavaScript" src="/excel/numeral.js"></script>
    <script language="JavaScript" src="/excel/numeric.js"></script>
    <script language="JavaScript" src="/excel/md5.js"></script>
    <script language="JavaScript" src="/excel/jstat.js"></script>
    <script language="JavaScript" src="/excel/formula.js"></script>
    <script language="JavaScript" src="/excel/parser.js"></script>
    <script language="JavaScript" src="/excel/ruleJS.js"></script>
    <script language="JavaScript" src="/excel/handsontable.formula.js"></script>
    <script language="JavaScript" src="/excel/bootstrap-tab.js"></script>
    <script language="JavaScript" src="/excel/sheet.ref.js"></script>
    <script language="JavaScript" src="/excel/color.js"></script>
    <script type="text/javascript">
        function initSheet(){
            $(".main-excel").excel({
                readOnly:false,
                readOnlyCell:',0,',
                onTabShow:resize,
                onClick : cancel
            });
            $(".ref_excel").excel({
                readOnly:true
            });
        }
        function loadSheet(config){
            $(".main-excel").excel("addTab" ,{cfg : config ,show:false});
        }
        function showSheet() {
            $(".main-excel").excel("show");
        }
        function addSheet(config ,show){
            $(".main-excel").excel("addTab" ,{cfg : config ,show:show});
        }
        function deleteSheet(){
            var data = $(".main-excel").excel("getActiveTabData");
            if( data ){
                if( data.id > 0){
                    loadMask();
                    $.ajax({
                        type: "POST",
                        url: "{{ url('admin/budget/delete')}}",
                        dataType: 'json',
                        cache: false,
                        data: {id : data.id },
                        success : function(){
                            $(".main-excel").excel("removeActiveTab");
                            unloadMask();
                        },
                        error : function(){
                            alert("删除失败");
                            unloadMask();
                        }
                    });
                }else {
                    $(".main-excel").excel("removeActiveTab");
                }
            }
        }
        function formula() {
            var data = $(".main-excel").excel("getActiveTabData");
            if( data ){
                if( data.id > 0){
                    window.location.href='/admin/formula/index?budget_id='+data.id+'&pid={{ Request::get('pid') }}&id={{ Request::get('id') }}';
                }
            }
        }
        function modifyed(){
            var data = $(".main-excel").excel("getActiveTabData");
            if( data ){
                if( data.id > 0){
                    window.location.href='/admin/formula/1/change?budget_id='+data.id+'&pid={{ Request::get('pid') }}&id={{ Request::get('id') }}';
                }
            }
        }
        function cancel(coords, TD){
            if(0 == coords.col){
                var param = {};
                param.row = coords.row;
                param.col = coords.col;
                param.text = "取消|业主未确认-承包单位未确认-咨询单位未确认";
                var data = this.getSourceDataAtRow( coords.row );
                if(data[0] > 0){
                    loadMask();
                    $.ajax({
                        type: "get",
                        url: "{{ url('admin/budget/cancel')}}/" + data[0],
                        dataType: 'json',
                        cache: false,
                        success:function(){
                            $('.main-excel').excel("setCellData" ,param);
                            unloadMask();
                        }
                        ,error:function(){
                            alert("取消失败");
                            unloadMask();
                        }
                    });
                }
            }
        }

        function saveSingleSheet(data ,index){
            var close = true;
            if( data.length - 1 > index){
                close = false;
            }
            do{
                if(undefined === data[index]){
                    alert("保存成功");
                    unloadMask();
                    break;
                }
                if(false === data[index]){
                    setTimeout( function(){
                        saveSingleSheet(data ,index + 1);
                    },1);
                    break;
                }
                $.ajax({
                    type: "POST",
                    url: "{{ url('admin/budget/save?project_id=') }}{{$project_id}}",
                    dataType: 'json',
                    processData: false,
                    contentType: "application/json",
                    cache: false,
                    data: JSON.stringify( data[ index ] ),
                    success:function(response){
                        if(response.success){
                            //$(".main-excel").excel("clear");
                            //for(var i = 0 ; i < response.sheets.length ; i++){
                            //    addSheet( response.sheets[i] ,false);
                            //}
                            //showSheet();
                            $(".main-excel").excel("update" ,{index : index ,config : response.sheets});
                            if( !close ){
                                setTimeout( function(){
                                    saveSingleSheet(data ,index + 1);
                                },1);
                            }
                        }
                        if( close ){
                            unloadMask();
                            alert("保存成功")
                        }
                    },
                    error:function(){
                        alert("保存失败");
                        unloadMask();
                    }
                })
            }while (0);
        }

        function saveSheet(){
            var data = $(".main-excel").excel("getAllChangedData");
            if(data){
                loadMask();
                saveSingleSheet( data ,0 )
            }
        }

        function resize(){
            var height = $("body").height() - $(".main-header").height() - $(".content-header").height() - 200;
            var width = $(".tab-content").width();
            $(".main-excel").excel("updateSettings" ,{width : width ,height:height});
        }

        function initMask(){
            $('#myModal').modal({backdrop: 'static', keyboard: false});
        }

        function loadMask(){
            $('#myModal').modal('show');
        }

        function unloadMask(){
            $('#myModal').modal('hide');
        }
        /*
        $(document).ajaxStart(function(){
            loadMask();
        });
        $(document).ajaxComplete(function(event, xhr, settings){
            unloadMask();
        });
        */
        function loadSingleSheet(urls ,i){
            if(i == urls.length){
                unloadMask();
                return;
            }
            $.ajax({
                type: "GET",
                url: "{{ url("admin/budget/sheet/") }}/" + urls[i],
                dataType: 'json',
                processData: false,
                contentType: "application/json",
                cache: false,
                success:function(response){
                    if(response.success){
                        loadSheet(response.sheets  ,false);
                        loadSingleSheet(urls ,i + 1)
                    }
                    if(i == urls.length - 1){
                        $(".main-excel").excel("show");
                    }
                },
                error:function(){
                    alert("加载出错" + urls[i]);
                    unloadMask();
                }
            })
        }
        $(document).ready(function () {
            initMask();
            loadMask();
            initSheet();
            var urls = [];
            @foreach($sheets as $key=>$sheet)
                urls.push( "{{$sheet}}" );
            @endforeach

            $("#button_add").bind("click",function(e){
                addSheet($.extend(true ,{} ,empty_sheet) ,true);
            });
            $('#dl').on('hidden.bs.modal', function (e) {

            });
            $(window).resize(function(){
                resize();
            });
            $("#color").color({
                onColor:function(color){
                    $(".main-excel").excel("setCellColor" ,color);
                }
            });
            $("#color").bind("click" ,function(){
                $(this).color("show");
            });
            $("#border").color({
                onColor:function(color){
                    $(".main-excel").excel("setBorderColor" ,color);
                }
            });
            $("#border").bind("click" ,function(){
                $(this).color("show");
            });

            loadSingleSheet(urls , 0);
        });
    </script>

@endsection

@section('pageHeader','后台管理系统')

@section('pageDesc','DashBoard')

@section('content')
    <div style="padding: 5px;">
        <button id="button_add" class="btn btn-default">新增</button>
        <button onclick="saveSheet();" class="btn btn-default" style="margin-left: 5px">保存</button>
        <button onclick="deleteSheet();" class="btn btn-default" style="margin-left: 5px">删除当前选中Sheet</button>
        <button onclick="refSheet();" class="btn btn-default" style="margin-left: 5px">引用表格</button>
        <a  href="{{ url('admin/budget/download')}}/{{$pid}}/{{$project_id}}" class="btn btn-default" style="margin-left: 5px">导出</a>
        <button id="color" class="btn btn-default" style="margin-left: 5px">背景颜色</button>
        <button id="border" class="btn btn-default" style="margin-left: 5px">边框颜色</button>
        <button onclick="formula();" class="btn btn-default" style="margin-left: 5px">查看反馈</button>
        <button onclick="modifyed();" class="btn btn-default" style="margin-left: 5px">查看修改记录</button>
    </div>
    <div class="main-excel excel">

    </div>
    <div class="modal" id="myModal">

    </div>
    {{--<div id="mask" style="position: fixed;top:0;height: 100%;width: 100%;opacity: 0.5;background-color: #0c0c0c;display: none;z-index: 999999"></div>--}}

    <div id="dl" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">引用表格</h4>
                </div>
                <div class="modal-body">
                    <div class="dropdown">
                        <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                            <span id="ht">选择合同</span>
                            <span class="caret"></span>
                        </button>
                        @if(count($settlements) > 0)
                            <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" style="height: 100px; overflow: auto;">
                                @foreach($settlements as $settlement)
                                    <li><a href="javascript:getRefSheet('{{ url('admin/budget/getsheets')}}/{{$settlement->id}}' ,'{{$settlement->name}}');">{{$settlement->name}}</a></li>
                                @endforeach
                            </ul>
                        @endif
                    </div>
                    <div class="ref_excel" style="height: 400px; margin-top: 5px;"></div>
                </div>
                <div class="modal-footer">
                    {{--<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>--}}
                    <button type="button" class="btn btn-primary" onclick="importAll()">导入所有Sheet</button>
                    <button type="button" class="btn btn-primary" onclick="importActive()">导入选中Sheet</button>
                    <button type="button" class="btn btn-primary" onclick="setRefData()">引用选中值</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
@endsection